package com.lvm.app.file;

import sun.awt.HKSCS;

import java.io.File;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

/**
 * 文件类型check
 */
public class FileTypeCheck {

    private final static Set<String> videoSuffix = new HashSet<String>(Arrays.asList("mp4","mov","wmv"));
    private final static Set<String> picSuffix = new HashSet<String>(Arrays.asList("png","jpeg","gif","jpg"));

    public static FileType checkFileType(File file){
            if(!file.isDirectory()){
                 if(videoSuffix.contains(getSuffix(file.getName()))){
                     return FileType.VIDEO;
                 }else if(picSuffix.contains(getSuffix(file.getName()))){
                     return FileType.PICTURE;
                 }else{
                     return FileType.OTHER;
                 }
            }
            return FileType.OTHER;
    }

    public static String getSuffix(String name){
        return name.toLowerCase().substring(name.lastIndexOf(".")+1);
    }

    public static int childVideoNumber(File file){
        int number =0;
        if(file.isDirectory()){
            if(file.listFiles()==null){
                return 0;
            }
            for(File f:file.listFiles()){
                if(f.isDirectory()&&(f.getName().indexOf("视频")>=0||f.getName().indexOf("video")>=0)){
                    if(f.listFiles()!=null){
                        for(File f2:f.listFiles()){
                            if(checkFileType(f2)==FileType.VIDEO){
                                number++;
                            }
                        }
                    }

                }else{
                    if(checkFileType(f)==FileType.VIDEO){
                        number++;
                    }
                }

            }
        }
        return number;

    }
    public static int childPicNumber(File file){
        int number =0;
        if(file.isDirectory()){
            if(file.listFiles()==null){
                return 0;
            }
            for(File f:file.listFiles()){
                if(f.isDirectory()&&(f.getName().indexOf("picture")>=0||f.getName().indexOf("图片")>=0)){
                    if(f.listFiles()!=null){
                        for(File f2:f.listFiles()){
                            if(checkFileType(f2)==FileType.PICTURE){
                                number++;
                            }
                        }
                    }
                }else{
                    if(checkFileType(f)==FileType.PICTURE){
                        number++;
                    }
                }

            }
        }
        return number;
    }

}
